package com.my.zl.console.controller.select;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.zl.console.common.aop.NoLogAnnotation;
import com.my.zl.console.common.constant.AutocodeRedisC;
import com.my.zl.console.controller.base.BaseController;
import com.my.zl.console.entity.autocode.ZlDict;
import com.my.zl.console.entity.autocode.ZlDictDtl;
import com.my.zl.console.entity.autocode.ZlFieldMapping;
import com.my.zl.console.entity.autocode.ZlProject;
import com.my.zl.console.entity.autocode.ZlProjectModule;
import com.my.zl.console.entity.autocode.ZlTable;
import com.my.zl.console.entity.autocode.ZlTableColumn;
import com.my.zl.console.entity.autocode.ZlTemplate;
import com.my.zl.console.entity.autocode.ZlTemplateDtl;
import com.my.zl.console.entity.autocode.ZlValidation;
import com.my.zl.console.entity.autocode.ZlWebComponent;
import com.my.zl.console.service.select.ISelectService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;

/**
 *  下拉选项，入口统一维护
 *  尽量只返回必须字段
 * @author ZL
 */
@Api(value="下拉选项")
@RestController
@RequestMapping("/api/autocode/select")
public class SelectController extends BaseController {

    @Autowired
    private ISelectService selectService;
    
    
    /**
     * 下拉选项-字典,以及其他
     */
    @SuppressWarnings("unchecked")
    @NoLogAnnotation
    @ApiOperation(value="下拉选项")
    @RequestMapping(value="/dict", method = RequestMethod.GET)
    public Object selectDict(String type) throws Exception{
        Map<String,Map<String, Object>> mapAll = new LinkedHashMap<>();
        selectDictHander(mapAll, type);
        return successDATA(mapAll);
    }
    private void selectDictHander(Map<String,Map<String, Object>> mapAll, String type) throws Exception{
        //字典明细
        List<ZlDictDtl> dataDictDtl = selectService.queryDictDtlForSelect(type);
        for(ZlDictDtl lt : dataDictDtl){
            if(mapAll.get(lt.getDictType()) != null){
                mapAll.get(lt.getDictType()).put(lt.getDictDtlNo().toString(), lt.getDictDisplay1());
            }else{
                Map<String, Object> map = new LinkedHashMap<>();
                map.put(lt.getDictDtlNo().toString(), lt.getDictDisplay1());
                mapAll.put(lt.getDictType().toString(), map);
            }
        }
    }
    /**
     * 下拉选项
     */
    @NoLogAnnotation
    @ApiOperation(value="下拉选项")
    @RequestMapping(value="/dictz", method = RequestMethod.GET)
    public Object selectDictz(String type,String key) throws Exception{
        Map<String,Map<String, Object>> mapAll = new LinkedHashMap<>();
        List<String> types = new ArrayList<>();
        if(StringUtils.isNotBlank(type)){
            types = ListUtil.toList(type.split(",")); 
        }
        String labelValue = "labelCode";
        
        if(types.contains("ZSELECT_DICT")){
            //字典类型
            List<ZlDict> dataDict = selectService.queryDictForSelect();
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlDict lt : dataDict){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getDictTitle());
                map.put(lt.getDictType().toString(), mapData);
            }
            mapAll.put("ZSELECT_DICT", map);
        }
        if(types.contains("ZSELECT_FIELD_MAPPING")){
            //映射信息
            List<ZlFieldMapping> data = selectService.queryFieldMappingForSelect();
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlFieldMapping lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getMappingTitle());
                map.put(lt.getMappingId().toString(), mapData);
            }
            mapAll.put("ZSELECT_FIELD_MAPPING", map);
        }
        if(types.contains("ZSELECT_PROJECT")){
            //项目 
            List<ZlProject> data = selectService.queryProjectForSelect();
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlProject lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getProjectTitle());
                map.put(lt.getProjectId().toString(), mapData);
            }
            mapAll.put("ZSELECT_PROJECT", map);
        }
        if(types.contains("ZSELECT_PROJECT_MODULE")){
            //项目模块
            List<ZlProjectModule> data = selectService.queryProjectModuleForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlProjectModule lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getModuleTitle());
                map.put(lt.getModuleId().toString(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_PROJECT_MODULE_"+lt.getProjectId().toString());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getModuleId().toString().toString(), mapData);
                mapAll.put("ZSELECT_PROJECT_MODULE_"+lt.getProjectId().toString(), mapchild);
            }
            mapAll.put("ZSELECT_PROJECT_MODULE", map);
        }
        if(types.contains("ZSELECT_VALIDATION")){
            //校验规则
            List<ZlValidation> data = selectService.queryValidationForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlValidation lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getValidationTitle());
                map.put(lt.getValidationId().toString(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_VALIDATION_"+lt.getProjectId().toString());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getValidationId().toString(), mapData);
                mapAll.put("ZSELECT_VALIDATION_"+lt.getProjectId().toString(), mapchild);
            }
            mapAll.put("ZSELECT_VALIDATION", map);
        }
        if(types.contains("ZSELECT_TABLE")){
            //表信息
            List<ZlTable> data = selectService.queryTableForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTable lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getTableTitle());
                map.put(lt.getTableId().toString(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TABLE_"+lt.getProjectId().toString());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getTableCode().toString(), mapData);
                mapAll.put("ZSELECT_TABLE_"+lt.getProjectId().toString(), mapchild);
            }
            mapAll.put("ZSELECT_TABLE", map);
        }
        if(types.contains("ZSELECT_TABLE_COLUMN")){
            //表信息
            List<ZlTableColumn> data = selectService.queryTableColumnForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTableColumn lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getColumnTitle());
                map.put(lt.getColumnId().toString(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TABLE_COLUMN_"+lt.getTableCode().toString());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getColumnCode().toString(), mapData);
                mapAll.put("ZSELECT_TABLE_COLUMN_"+lt.getTableCode().toString(), mapchild);
            }
            mapAll.put("ZSELECT_TABLE_COLUMN", map);
        }
        if(types.contains("ZSELECT_TEMPLATE")){
            //模板信息
            List<ZlTemplate> data = selectService.queryTemplateForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTemplate lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getTemplateTitle());
                map.put(lt.getTemplateId().toString(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TEMPLATE_"+lt.getModuleId().toString());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getTemplateId().toString(), mapData);
                mapAll.put("ZSELECT_TEMPLATE_"+lt.getModuleId().toString(), mapchild);
            }
            mapAll.put("ZSELECT_TEMPLATE", map);
        }
        if(types.contains("ZSELECT_TEMPLATE_DTL")){
            //模板信息
            List<ZlTemplateDtl> data = selectService.queryTemplateDtlForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlTemplateDtl lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getFileTitle());
                map.put(lt.getTemplateId().toString(), mapData);
                
                Map<String,Object> mapchild = mapAll.get("ZSELECT_TEMPLATE_DTL_"+lt.getTemplateId().toString());
                if(mapchild == null){
                    mapchild = new LinkedHashMap<>();
                }
                mapchild.put(lt.getTemplateFileId().toString(), mapData);
                mapAll.put("ZSELECT_TEMPLATE_DTL_"+lt.getTemplateId().toString(), mapchild);
            }
            mapAll.put("ZSELECT_TEMPLATE_DTL", map);
        }
        if(types.contains("ZSELECT_WEB_COMPONENT")){
            //前端组件信息
            List<ZlWebComponent> data = selectService.queryWebComponentForSelect(null);
            Map<String,Object> map = new LinkedHashMap<>();
            for(ZlWebComponent lt : data){
                Map<String,Object>  mapData = BeanUtil.beanToMap(lt);
                mapData.put(labelValue, lt.getComponentTitle());
                map.put(lt.getComponentId().toString(), mapData);
            }
            mapAll.put("ZSELECT_WEB_COMPONENT", map);
        }
        return successDATA(mapAll);
    }
}